<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/10 0010
 * Time: 15:15
 */

namespace Cohuna\Controllers;

use Cohuna\Models\CohunaPermissionResource;
use Cohuna\Models\CohunaPermissionRole;
use Cohuna\Paginator\PaginatorArray;

/**
 * 用户角色管理
 * @package Cohuna\Controllers
 */
class RoleController extends ControllerBase
{
    public function listAction()
    {
        $page = max(intval($this->request->getQuery('page')),1);
        $keyword = $this->request->getQuery('keyword');
        $condition = array('role_id > 0');
        if(empty($keyword) === false){
            $condition[0] .= ' AND role_name=:keyword:';
            $condition['bind']['keyword'] = $keyword;
        }
        $offset = ($page - 1) * self::PageSize;

        $totalCount = CohunaPermissionRole::count($condition);

        $condition['offset'] = intval($offset);
        $condition['limit'] = self::PageSize;
        $condition['order'] = 'role_id DESC';

        $list = CohunaPermissionRole::find($condition);

        $paginator = new PaginatorArray([
            'data'          => $list,
            'limit'         => self::PageSize,
            'current'       => $page,
            'total_items'   => $totalCount,
            'href'          => '/role/list'
        ]);
        $this->view->setVar('Model',$paginator);
    }

    public function editAction()
    {
        $role_id = intval($this->request->getPost('role_id'));
        if($role_id > 0){
            $role = CohunaPermissionRole::findFirst($role_id);
            if(empty($role)){
                $this->dispatcher->forward(array('controller'=>'Error','action'=>'show404'));
            }
        }else{
            $role = new CohunaPermissionRole();
        }

        $this->view->setVar('ResourceList',CohunaPermissionResource::getPermissionResourceGroup());

        $this->view->setVar('Model',$role);
    }

    public function delActon()
    {

    }

}